using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;
using UnityEngine.UI;
using XLua;
using SgsUI;

namespace SgsUI
{
    public class ViewCanvas : MonoBehaviour
    {
        public string output_path = "output";
        int GetWidth()
        {
            if(TryGetComponent(out RectTransform rect)){
                return (int)rect.sizeDelta.x;
            }

            return 0;
        }

        int GetHeight()
        {
            if(TryGetComponent(out RectTransform rect)){
                return (int)rect.sizeDelta.y;
            }

            return 0;
        }

        public void OutputLua()
        {      
            LuaManager luaManager = LuaManager.Instance;
            luaManager.SendLuaEvent("RebuildTextFormat");
            // LuaTable outString = luaManager.luaEnv.NewTable();
            luaManager.luaEnv.Global.Get("Lang",out LuaTable outString);

            LuaHelper.Instance.SetLang(outString);

            List<NodeData> outputData = new List<NodeData>();
            Transform t = transform;

            for (int i = 0; i < t.childCount; i++)
            {
                GameObject obj = t.GetChild(i).gameObject;
                if(obj.TryGetComponent(out ViewNode viewNode))
                {
                    NodeData nodeData = new NodeData();
                    viewNode.GetParamData(nodeData);
                    outputData.Add(nodeData);
                }
            }

            LuaOutputer.OutputLua(GetWidth(),GetHeight(),outputData,output_path);
        }

        public void InputLua(string path)
        {
            LuaManager luaManager = LuaManager.Instance;
            luaManager.Reload();
            luaManager.SendLuaEvent("LoadUIFromConf",gameObject,path);
        }
    }
}